package com.foreknow.elm.controller;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;



public class CartController {


    /**
     * 请求地址  http://localhost:8080/MavenElmWebProject/CartController/listCart?userId=183333333&businessId=10001
     * @param request
     * @return
     */
    public List<Cart> listCart(HttpServletRequest request) {
        CartService cartService = new CartServiceImpl();
        String userId = request.getParameter("userId");
        String businessId = request.getParameter("businessId");
        Cart cart = new Cart();
        cart.setUserId(userId);
        if (businessId != null) {
            cart.setBusinessId(Integer.valueOf(businessId));
        }
        List<Cart> list = cartService.listCart(cart);
        return list;
    }
    /**
     * 请求地址  http://localhost:8080/MavenElmWebProject/CartController/saveCart?userId=183333333&foodId=2&businessId=10001
     * @param request
     * @return
     */
    public int saveCart(HttpServletRequest request){
        CartService service = new CartServiceImpl();
        String userId = request.getParameter("userId");
        String businessId = request.getParameter("businessId");
        String foodId = request.getParameter("foodId");
        int add = service.saveCart(userId,Integer.valueOf(businessId),Integer.valueOf(foodId));
        return add;
    }
    /**
     * http://localhost:8080/MavenElmWebProject/CartController/updateCart?userId=18041339018&foodId=2&businessId=10001&quantity=3
     * 功能：根据用户编号、商家编号、食品编号更新数量
     * @param request
     * @return
     */
    public Integer updateCart(HttpServletRequest request){
        Integer update=0;
        CartService service=new CartServiceImpl();
        String userId= request.getParameter("userId");
        String businessId= request.getParameter("businessId");
        String foodId= request.getParameter("foodId");
        String quantity= request.getParameter("quantity");
        update= service.updateCart(userId,Integer.valueOf(businessId),Integer.valueOf(foodId),Integer.valueOf(quantity));
        return update;
    }

    /**
     * http://localhost:8080/MavenElmWebProject/CartController/removeCart?userId=183333333&businessId=10001&foodId=2
     * 功能：根据用户编号、商家编号、食品编号删除购物车表中的一条食品记录
     * ​ 根据用户编号、商家编号删除购物车表中的多条条记录
     * @param request
     * @return
     */
    public Integer removeCart(HttpServletRequest request){
        Integer del=0;
        CartService service=new CartServiceImpl();
        String userId= request.getParameter("userId");
        String businessId= request.getParameter("businessId");
        String foodId= request.getParameter("foodId");
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(Integer.valueOf(businessId));
        if (foodId != null) {
            cart.setFoodId(Integer.parseInt(foodId));
        }
        del=service.removeCart(cart);
        return del;
    }

}
